<?php
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
$link=connect();

if(!isset($_GET['id'])) {
    header("Location: simpleTable.php");
    exit;
}

$id = intval($_GET['id']);

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = escape($link, $_POST);
    $query = "UPDATE member SET 
              username='{$data['username']}', 
              pw='{$data['pw']}', 
              sex='{$data['sex']}', 
              phonenum='{$data['phonenum']}', 
              address='{$data['address']}', 
              email='{$data['email']}' 
              WHERE id=$id";
    execute($link, $query);
    header("Location: simpleTable.php");
    exit;
}

$query = "SELECT * FROM member WHERE id=$id";
$result = execute($link, $query);
$data = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑用户</title>
    <link rel="stylesheet" href="../css/table.css">
</head>
<body>
    <h2 class="page-title">编辑用户</h2>
    <div class="form-container">
        <form method="post">
            <div class="form-group">
                <label>用户名:</label>
                <input type="text" name="username" value="<?=$data['username']?>" required>
            </div>
            <div class="form-group">
                <label>密码:</label>
                <input type="password" name="pw" value="<?=$data['pw']?>" required>
            </div>
            <div class="form-group">
                <label>性别:</label>
                <input type="text" name="sex" value="<?=$data['sex']?>">
            </div>
            <div class="form-group">
                <label>电话:</label>
                <input type="text" name="phonenum" value="<?=$data['phonenum']?>">
            </div>
            <div class="form-group">
                <label>地址:</label>
                <input type="text" name="address" value="<?=$data['address']?>">
            </div>
            <div class="form-group">
                <label>邮箱:</label>
                <input type="email" name="email" value="<?=$data['email']?>">
            </div>
            <div class="form-buttons">
                <button type="submit" class="edit-btn">更新</button>
                <a href="simpleTable.php" class="delete-btn" style="margin-left:10px;">返回</a>
            </div>
        </form>
    </div>
</body>
</html>
